﻿using PMSystem.DataAccess;
using PMSystem.DataAccess.DataRepository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PMSystem.ApplicationLogics
{
   public class RecordItemService
    {
       public void DeleteRecord(int id)
       {
           try { 
           using (var repo = new RecordItemRepository())
           {
               var record = repo.GetById(id);
               if (record != null)
               {
                   using (var repoimg = new RecordItemImageRepository())
                   {
                       var imgList = repoimg.GetAllByRecordId(id);
                       if (imgList != null && imgList.Count > 0)
                       {
                           repoimg.DeleteList(imgList);
                       }
                   }
                   record.IsDeleted = true;
                   repo.InsertOrUpdate(record);
                   repo.Save();
               }
           }
           }
           catch (Exception ex)
           {
               var errr = ex.Message;
           }
       }

       public List<RecordItem> GetRecordsBySearch(string keyword)
       {
           List<RecordItem> results = new List<RecordItem>();
           using (var repo = new RecordItemRepository())
           {
               results = repo.GetAll.ToList();
           }
           if (!string.IsNullOrWhiteSpace(keyword))
           {
               results = results.Where(x => 
                   
                   (
                   (x.RackNumber!=null && x.RackNumber.ToLower().Contains(keyword.ToLower()))
                   ||
                   (x.RecordNumber!=null && x.RecordNumber.ToLower().Contains(keyword.ToLower()))
                    ||
                     (x.TrayNumber!=null && x.TrayNumber.ToLower().Contains(keyword.ToLower()))
                    ||
                     (x.Title!=null && x.Title.ToLower().Contains(keyword.ToLower()))
                    ||
                     (x.Author!=null && x.Author.ToLower().Contains(keyword.ToLower()))
                    ||
                   (x.Description!=null && x.Description.ToLower().Contains(keyword.ToLower()))
                    ||
                   (x.CreatedBy!=null && x.CreatedBy.ToLower().Contains(keyword.ToLower()))
                   )
                   && x.IsDeleted == false
                   ).ToList();
           }

           return results;
       }

    }


}
